import os

from api.TimeProcess import *
#from api.ZipEncrypeFile import *
from api.Byteencrypt import *
import functools

def clock(func):
    """this is outer clock function"""

    @functools.wraps(func)  # --> 4
    def clocked(*args, **kwargs):  # -- 1
        """this is inner clocked function"""
        start_time = time.time()
        result = func(*args, **kwargs)  # --> 2
        time_cost = time.time() - start_time
        print(func.__name__ + " func time_cost -> {}".format(time_cost))
        return result
    return clocked  # --> 3

@functools.lru_cache()
@clock
def encode_file(des_path,now_time,time_cross,key):
    passwd = encryp_sha1(str(now_time) + str(time_cross) + key, key)

    # 提示：1小时=3600秒，1天=86400秒，一年=31536000秒。
    # time.ctime(1629798961+157680000)

    #压缩
    #compress_to_zip([r'H:\helo.txt'], passwd=passwd)

    #异或
    from_path = des_path
    out_path = r'./addtion.lock'
    byteen(from_path,passwd,out_path)

    # 驳接
    from_path = r'./secret.lock'
    out_path = out_path
    write_ahead(int(now_time), time_cross, from_path, out_path)
    # read_res = read_encrypt_file(des_path)
    # print(analyse_data(source_path))


    #删除垃圾
    #os.remove(source_path)
    #os.remove(r'./output.zip')
    print ('Done')

@functools.lru_cache()
@clock
def decode_file(key):
    # 第一步获取网络时间，目前还没做。

    secret_path = './secret.lock'
    time_info = analyse_data(secret_path)
    print(time_info)
    passwd = encryp_sha1(str(time_info['timestamp']) + str(time_info['time_across']) + key, key)  # 获取密码
    print(passwd)
    #将100byte以后的内容作为文件保存
    file = read_late_encrypt(secret_path)
    with open (r'my_encode.zip','wb') as f:
        f.write(file)

    #异或解密
    bytede(passwd)
    #解压解密
    #unpress_zip(passwd=passwd)

    #清理垃圾
    #os.remove('./my_decode.zip')
    #os.remove('./my_encode.zip')
    print ('Done')



now_time = int(time.time())
time_cross = 500
key = 'mylove'
mypath = r'C:\Users\35992\Desktop\tt\testfile\附件4：概详设计说明书模板V1.0.doc'

encode_file(mypath,now_time,time_cross,key)

decode_file(key)